define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'scanwork/traceability/index' + location.search,
                    add_url: 'scanwork/traceability/add',
                    edit_url: 'scanwork/traceability/edit',
                    del_url: 'scanwork/traceability/del',
                    multi_url: 'scanwork/traceability/multi',
                    export_url: 'scanwork/traceability/export',
                    table: 'traceability',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'trace_code', title: __('溯源码'), operate: 'LIKE'},
                        {field: 'order_id', title: __('关联订单'), operate: 'LIKE'},
                        {field: 'product_name', title: __('产品名称'), operate: 'LIKE'},
                        {field: 'product_model', title: __('产品型号'), operate: 'LIKE'},
                        {field: 'customer_name', title: __('客户名称'), operate: 'LIKE'},
                        {field: 'production_start_time', title: __('生产开始时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'production_end_time', title: __('生产结束时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('创建时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, formatter: function(value, row, index) {
                            var table = this.table;
                            var options = table ? table.bootstrapTable('getOptions') : {};
                            var buttons = [];
                            buttons.push({
                                name: 'query',
                                text: __('溯源查询'),
                                title: __('溯源查询'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-search',
                                url: 'scanwork/traceability/query/ids/{ids}',
                                callback: function (data) {
                                    table.bootstrapTable('refresh');
                                }
                            });
                            buttons.push({
                                name: 'generate',
                                text: __('生成溯源码'),
                                title: __('生成溯源码'),
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-magic',
                                url: 'scanwork/traceability/generate/ids/{ids}',
                                callback: function (data) {
                                    table.bootstrapTable('refresh');
                                }
                            });
                            return Table.api.formatter.operate.call(this, value, row, index, buttons);
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        query: function () {
            Controller.api.bindevent();
        },
        generate: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
